package com.javaweb.util.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.ibm.icu.math.BigDecimal;

public class PoiXwpfWordTemplateUtil {
	
	/**
	采用{{ADDRESS_COUNTRY}}占位符替换
	如果是表格，写法参考如下：
	{{LIST}}序号	 工种  	人数
       [XH]	     [WT]	[RS]
	*/
	public static void main(String[] args) throws Exception {
		Map<String,Object> map = new HashMap<>();
		map.put("INSURED_NAME","张三");
		map.put("ADDRESS_COUNTRY","富强路18号");
		map.put("DEDUCTIBLE_TEXT","AaBbCcDdEeFfGg");
		map.put("SPECIAL_AGGREMENT",new BigDecimal("202313.2313"));
		List<Map<String,Object>> tableList = new ArrayList<>();  
        Map<String, Object> data1 = Map.of("XH",1,"WT","工种A","RS",100);  
        Map<String, Object> data2 = Map.of("XH",2,"WT","工种B","RS",200);  
        Map<String, Object> data3 = Map.of("XH",3,"WT","工种C","RS",300);  
        tableList.add(data1);tableList.add(data2);tableList.add(data3);
        map.put("LIST",tableList);
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
		InputStream inputStream = new FileInputStream(new File("C:\\Users\\admin\\Desktop\\1.docx"));
		Configure build = Configure.builder().bind(policy,new String[]{"LIST"}).build();
		XWPFTemplate render = XWPFTemplate.compile(inputStream,build).render(map);
        File.createTempFile("C:\\Users\\admin\\Desktop\\2.docx",null);
        render.writeToFile("C:\\Users\\admin\\Desktop\\2.docx");
	}

}
